
using System;
using System.Collections;

using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;

using needle.UI.Controls.Month;

namespace needle.Appointments {

    /// <summary>
    /// A custom CollectionEditor for editing DateItemCollection
    /// </summary>
    public class AppointmentCollectionEditor : CollectionEditor {
        #region private class member

        private MonthCalendar m_calendar;
        private ITypeDescriptorContext m_context;

        #endregion


        #region Constructor

        public AppointmentCollectionEditor(Type type)
            : base(type) {

        }

        #endregion

        #region overrides

        protected override void DestroyInstance(object instance) {
            base.DestroyInstance(instance);

        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
            m_context = context;
            //MonthCalendar originalControl = (MonthCalendar) context.Instance;
            //m_calendar = originalControl;

            object returnObject = base.EditValue(context, provider, value);

            AppointmentCollection collection = returnObject as AppointmentCollection;
            if (collection != null) {
                collection.ModifiedEvent();
            }

            return returnObject;
        }


        protected override object CreateInstance(Type itemType) {
            object dateItem = base.CreateInstance(itemType);

            MonthCalendar originalControl = (MonthCalendar)m_context.Instance;
            m_calendar = originalControl;

            ((Appointment)dateItem).Date = DateTime.Today;
            ((Appointment)dateItem).Calendar = m_calendar;
            return dateItem;
        }

        #endregion
    }

}
